package guoanmaker.operator.business.controller;

import guoanmaker.operator.business.model.AppOutServerClass;
import guoanmaker.operator.business.model.OperatorHotServer;
import guoanmaker.operator.business.model.OperatorServerClass;
import guoanmaker.operator.business.service.OperatorHotServerServiceInterface;
import guoanmaker.operator.business.service.OperatorServerClassServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *  
 *
 * @author huZhaoyu 
 * @ClassName: OperatorServerClassController
 * @Description: OperatorServerClassController 
 * @date 2017/7/9 上午10:11 
 */
@RestController
@RequestMapping(ApiController.OPERATOR_SERVER_CLASS)
public class OperatorServerClassController {
    private final AjaxJson ajaxJson;
    private final OperatorServerClassServiceInterface operatorServerClassServiceInterface;
    @Autowired
    private  OperatorHotServerServiceInterface operatorHotServerServiceInterface;
    public OperatorServerClassController(AjaxJson ajaxJson, OperatorServerClassServiceInterface operatorServerClassServiceInterface) {
        this.ajaxJson = ajaxJson;
        this.operatorServerClassServiceInterface = operatorServerClassServiceInterface;
    }

   
   
    /**
     * @return OperatorServerClass    
     * @Title: getServerClassById 
     * @Description: 根据Id查询服务分类
     * @param: String id   
     * @throws 
     */
    @ApiOperation(value = "根据Id查询服务分类", notes = "根据Id查询服务分类")
    @PostMapping(value = "getServerClassById")
    public AjaxJson getServerClassById(@RequestParam(value = "id") String id) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorServerClassServiceInterface.getServerClassById(id));
    }

    /**
     * @return AjaxJson   
     * @Title: getAllServerClass
     * @Description: 查找所有服务分类 
     * @throws 
     */
    @ApiOperation(value = "查找所有服务分类", notes = "查找所有服务分类")
    @GetMapping(value = "getAllServerClass")
    public AjaxJson getAllServerClass() {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorServerClassServiceInterface.getAllServerClass());
    }

    /**
     * @return AjaxJson   
     * @Title: getAllServerClassByPage 
     * @Description: 查询所有服务分类并分页
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "分页查询服务分类", notes = "分页查询服务分类")
    @GetMapping(value = "getAllServerClassByPage")
    public AjaxJson getAllServerClassByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                            @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorServerClassServiceInterface.getAllServerClassByPage(page, size));
    }

    /**
     * @return AjaxJson   
     * @Title: getByParentId 
     * @Description: 根据父Id查询所有服务分类
     * @param: String parentServerId 父Id
     */
    @ApiOperation(value = "根据父Id查询", notes = "根据父Id查询")
    @GetMapping(value = "getByParentId")
    public AjaxJson getByParentId(@RequestParam(value = "parentServerId") String parentServerId) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorServerClassServiceInterface.getByParentServerId(parentServerId));
    }
    
    
    /**
     * @return AjaxJson   
     * @Title: getByParentId 
     * @Description: 根据父Id查询所有服务分类
     * @param: String parentServerId 父Id
     */
    @ApiOperation(value = "根据父Id查询2级列表", notes = "根据父Id查询2级列表")
    @PostMapping(value = "getByParentIdtwoList")
    public AjaxJson getByParentIdtwoList(String parentServerId) {
    	try{
    	List<OperatorServerClass>  serclasses=operatorServerClassServiceInterface.getByParentServerId(parentServerId);
    	if(serclasses.size()!=0){
    		ajaxJson.setSuccess(true).setStatus(200).setData(serclasses);
    	}else{
    		ajaxJson.setSuccess(true).setStatus(200).setData(serclasses);
    	}
    	}catch(Exception e){
    		ajaxJson.setSuccess(true).setStatus(200).setData(null);
    	}
        return ajaxJson;
    }
    
    /**
     * @return AjaxJson   
     * @Title: getAllServerClass
     * @Description: 查找所有服务分类 
     * @throws 
     */
    @ApiOperation(value = "查找app所有服务分类", notes = "查找app所有服务分类")
    @GetMapping(value = "getAllAppServerClass")
    public AjaxJson getAllAppServerClass() {
    	try{
    	List<AppOutServerClass> apps=operatorServerClassServiceInterface.getAllServerClassByFirst();
    	ajaxJson.setSuccess(true).setStatus(200).setData(apps);
    	}catch(Exception e){
    		e.printStackTrace();
    		ajaxJson.setSuccess(false).setStatus(250).setMsg("没有数据");
    	}
    	
        return ajaxJson;
    }
    
    
    @ApiOperation(value = "查找所有1级务分类加商户信息", notes = "查找所有1级务分类加商户信息")
    @PostMapping(value = "getAllServerClassFirstLevel")
    public AjaxJson getAllServerClassFirstLevel(String parentServerId) {
    	try{
    	List<OperatorServerClass> apps=operatorServerClassServiceInterface.getAllServerClassByFirstlevel(parentServerId);
    	ajaxJson.setSuccess(true).setStatus(200).setData(apps);
    		
    	}catch(Exception e){
    		ajaxJson.setSuccess(false).setStatus(250).setMsg("没有数据").setData(null);
    	}
    		
    	
        return ajaxJson;
    }
    
    @ApiOperation(value = "查找所有2级务", notes = "查找所有2级服务")
    @PostMapping(value = "getAllServerClassTwoLevel")
    public AjaxJson getAllServerClassTwoLevel() {
    	try{
    	List<OperatorServerClass> apps=operatorServerClassServiceInterface.getAllServerClassTwoLevel();
    	ajaxJson.setSuccess(true).setStatus(200).setData(apps);
    	}catch(Exception e){
    		e.printStackTrace();
    		ajaxJson.setSuccess(false).setStatus(250).setMsg("没有数据");
    	}
    	
        return ajaxJson;
    }
    
    
    @ApiOperation(value = "查找所有1级务", notes = "查找所有1级服务")
    @PostMapping(value = "getAllServerClassOneLevel")
    public AjaxJson getAllServerClassOneLevel() {
    	try{
    	List<OperatorServerClass> apps=operatorServerClassServiceInterface.getAllServerClassFirstLevel();
    	ajaxJson.setSuccess(true).setStatus(200).setData(apps);
    	}catch(Exception e){
    		ajaxJson.setSuccess(false).setStatus(250).setMsg("没有数据").setData(null);
    	}
    	
        return ajaxJson;
    }
    
    
    @ApiOperation(value = "pc端保存服务分类", notes = "新增/修改服务分类")
    @PostMapping(value = "saveServerClassPc")
    public AjaxJson saveServerClassPc(@RequestBody OperatorServerClass operatorServerClass) {
       try{
    	   
    	   if(operatorServerClass.getId()!=null&&!operatorServerClass.getId().equals("")){
       		operatorServerClass.setAlterTime(new Date());
       	}else{
       		Date ss=new Date();
       		operatorServerClass.setAlterTime(ss);
       		operatorServerClass.setCreateTime(ss);
       		operatorServerClass.setDeleteState("0");
       	}
    	   OperatorServerClass oc=null;
    	
    	if(operatorServerClass.getParentServerId()==null||operatorServerClass.getParentServerId().equals("")){//一级服务新建或更新
    		operatorServerClass.setServerLevel(1);
    	}else{
    		OperatorServerClass operSer=operatorServerClassServiceInterface.getServerClassById(operatorServerClass.getParentServerId());
    		if(operSer.getServerLevel()!=null){
    		operatorServerClass.setServerLevel(operSer.getServerLevel()+1);}
    		
    	}
    	
    	 oc = operatorServerClassServiceInterface.saveServerClass(operatorServerClass);
        ajaxJson.setSuccess(true).setStatus(200).setData(oc).setMsg(null);
    	}catch(Exception e){
    		ajaxJson.setSuccess(true).setStatus(250).setData(null).setMsg(null);
    	}
        return ajaxJson;
    }
    
    @ApiOperation(value = "查找所有1级务", notes = "查找所有1级服务")
    @PostMapping(value = "aaaaaaa")
    public AjaxJson aaaaaaa(String serverId) {
    	operatorServerClassServiceInterface.aaa(serverId);
    	ajaxJson.setSuccess(true).setStatus(200).setData(null);
        return ajaxJson;
    }
    
    /**
     * lihongyu
    * @Title: deleteServerClass 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param serverid
    * @return    设定文件 
    * @return AjaxJson    返回类型 
    * @throws 
     */
    @ApiOperation(value = "删除服务", notes = "删除服务")
    @PostMapping(value = "deleteServerClass")
    public AjaxJson deleteServerClass(String serverid) {
    	try{
    	Map apps=operatorServerClassServiceInterface.selectValitaByid(serverid);
    	ajaxJson.setSuccess(true).setStatus(200).setData(apps);
    	}catch(Exception e){
    		e.printStackTrace();
    		ajaxJson.setSuccess(false).setStatus(250).setMsg("系统错误").setData(null);
    	}
    	
        return ajaxJson;
    }
   
}
